﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ServiceGerenciador.Model;

public partial class paginas_Servicos : System.Web.UI.Page
{
    protected int ModelId
    {
        get
        {
            if (Request.QueryString["idServico"] != null)
            {
                return Convert.ToInt32(Request.QueryString["idServico"]);
            }
            else
            {
                return 0;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {      
        Initialize();        
    }
   
   
    private void Initialize()
    {
        if (!IsPostBack)
        {
            if (this.ModelId > 0)
            {
                Service service = new Service();
                service = Service.Find(ModelId);                
                
                txtServico.Text = Convert.ToString(service.servico);                    
                txtPreco.Text = Convert.ToString(service.preco);                                    
                txtTempo.Text = String.Format("{0:HH:mm}", service.tempo);    
            }
        }
    }


    protected void btnSalvar_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (ModelId > 0)
            {
                //incluir serviço
                Service service = new Service();
                service = Service.Find(ModelId);                       
                service.servico = txtServico.Text;
                service.preco = Convert.ToDecimal(txtPreco.Text);
                service.tempo = Convert.ToDateTime(txtTempo.Text);                
                service.Update();

                ApplicationEnvironment.Instance.Flash["Notice"] = "Serviço atualizado com sucesso!";

                Response.Redirect("Servicos.aspx");
            }
            else
            {
                Service service = null;
                service = new Service();

                service.servico = txtServico.Text;
                service.preco = Convert.ToDecimal(txtPreco.Text);
                service.tempo = Convert.ToDateTime(txtTempo.Text);
                service.Save();

                ApplicationEnvironment.Instance.Flash["Notice"] = "Serviço cadastrado com sucesso!";

                Response.Redirect("Servicos.aspx");
            }        
        }
    }
    
    protected void btnCancelar_Click(object sender, EventArgs e)
    {
        Response.Redirect("Servicos.aspx");
    }
    protected void btnExcluir_Click(object sender, EventArgs e)
    {
        Service service = Service.Find(ModelId);
        service.Delete();

        ApplicationEnvironment.Instance.Flash["Notice"] = "Serviço excluído com sucesso!";

        Response.Redirect("Servicos.aspx");
    }
}

